using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace CauBeTimMe.objects
{
    class Button
    {
        public event EventHandler Clicked = delegate { };
        protected Texture2D imgP, imgR;
        protected Rectangle rect, rectp, currect;
        public Boolean isVisible = true;
        protected Boolean pressed = false;

        public Button(Texture2D img1, Texture2D img2)
        {
            this.imgP = img1;
            this.imgR = img2;
            this.rect = imgR.Bounds;
            this.rectp = imgP.Bounds;
            this.currect = img1.Bounds;
        }

        public Point Location
        {
            set { rect.Location = value; }
            get { return rect.Location; }
        }

        public Point CurLocation
        {
            set { currect.Location = value; }
            get { return currect.Location; }
        }

        protected Boolean isContain(int x, int y)
        {
            if ((x > rect.X && x < rect.X + rect.Width) &&
                (y > rect.Y && y < rect.Y + rect.Height))
                return true;
            else return false;
        }

        public virtual void render(SpriteBatch spBatch)
        {
            if (!this.isVisible) return;
            if (pressed)
            {
                currect = rectp;
                currect.Location = new Point(rect.Location.X + 5, rect.Location.Y + 5);
                spBatch.Draw(imgP, currect, Color.White);
            }
        }

        public void touchEvent(TouchLocation touch)
        {
            if (!this.isVisible) return;
            if (touch.State == TouchLocationState.Pressed)
            {
                buttonPressed(touch);
            }
            else if (touch.State == TouchLocationState.Released)
            {
                buttonReleased(touch);
            }
            else if (touch.State == TouchLocationState.Moved)
            {
                buttonMoved(touch);
            }
        }

        private void buttonReleased(TouchLocation touch)
        {
            if (isContain((int)touch.Position.X, (int)touch.Position.Y))
            {
                Clicked(this, new EventArgs());
                pressed = false;
            }
        }

        private void buttonPressed(TouchLocation touch)
        {
            if (isContain((int)touch.Position.X, (int)touch.Position.Y))
            {
                pressed = true;
            }
        }

        private void buttonMoved(TouchLocation touch)
        {
            if (isContain((int)touch.Position.X, (int)touch.Position.Y))
            {
                pressed = true;
            }
            else
            {
                pressed = false;
            }
        }

    }
}
